//
//  TGLocationReachability.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/4/11.
//

#import "TGLocationReachability.h"

@interface TGLocationReachability()
@property (nonatomic,strong) LocalConnection * lc;

@end
@implementation TGLocationReachability
+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static TGLocationReachability *_manager = nil;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager ;
}
- (instancetype)init {
    if (self = [super init]) {
        self.status = -1;
        
//        [[NSNotificationCenter defaultCenter] addObserver:self
//                                                 selector:@selector(networkChanged:)
//                                                     name:kLocalConnectionInitializedNotification
//                                                   object:nil];
//
//        [[NSNotificationCenter defaultCenter] addObserver:self
//                                                 selector:@selector(networkChanged:)
//                                                     name:kLocalConnectionChangedNotification
//                                                   object:nil];
//        _lc = [[LocalConnection alloc] init];
//        [_lc startNotifier];
//        LocalConnectionStatus status = [_lc currentLocalConnectionStatus];
//        _status = status;
        
        
    }
    return self;
}

+(BOOL)isReachable {
    if ([TGLocationReachability shareInstance].status == LC_UnReachable) {
        return NO;
    }
    return YES;
}
- (void)networkChanged:(NSNotification *)notification
{
    LocalConnection *reachability = (LocalConnection *)notification.object;
    _status = [reachability currentLocalConnectionStatus];
    //    ReachabilityStatus previousStatus = [reachability previousReachabilityStatus];
    //    NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
    if (self.statusBlock) {
        self.statusBlock(_status);
    }
    //    if (status == RealStatusNotReachable)
    //    {
    //        self.flagLabel.text = @"Network unreachable!";
    //    }
    //
    //    if (status == RealStatusViaWiFi)
    //    {
    //        self.flagLabel.text = @"Network wifi! Free!";
    //    }
    //
    //    if (status == RealStatusViaWWAN)
    //    {
    //        self.flagLabel.text = @"Network WWAN! In charge!";
    //    }
    //
    //    WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
    //
    //    if (status == RealStatusViaWWAN)
    //    {
    //        if (accessType == WWANType2G)
    //        {
    //            self.flagLabel.text = @"RealReachabilityStatus2G";
    //        }
    //        else if (accessType == WWANType3G)
    //        {
    //            self.flagLabel.text = @"RealReachabilityStatus3G";
    //        }
    //        else if (accessType == WWANType4G)
    //        {
    //            self.flagLabel.text = @"RealReachabilityStatus4G";
    //        }
    //        else
    //        {
    //            self.flagLabel.text = @"Unknown RealReachability WWAN Status, might be iOS6";
    //        }
    //    }
}

@end
